<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :	11 February 2010
// version :	11 February 2010

class Cmis_Repository_Workspaces implements IteratorAggregate, Countable
{
	/**
	 * @var array
	 */
    protected $_repositories;

    /**
     * 
     * @param $response response with workspace data
     * @return Cmis_Repository_Workspaces
     */
    public function __construct(SimpleXMLIterator $response)
    {
        $this->_repositories = array();
    	foreach ($response->workspace as $workspace)
    	{
    		$info = new Cmis_Repository_Info($workspace);
    		$this->_repositories[ $info->getObjectId()->getId() ] = $info;
    	}

    	ksort($this->_repositories);
    }

    public function count()
    {
    	return count($this->_repositories);
    }

    /**
     * Get a repository by his id
     * @param string|Cmis_Repository_Id $repositoryId id of a repository, if null return all the repositories
     * @return Cmis_Repository_Info|array|null null if not found
     */
    public function getRepositoryById($repositoryId = null)
    {
    	if (is_null($repositoryId))
    	{
    		return $this->_repositories;
    	}
    	elseif (is_a($repositoryId, 'Cmis_Repository_Id'))
    	{
    		$repositoryId = $repositoryId->getId();
    	}

        if (isset($this->_repositories[ $repositoryId ]))
        {
            return $this->_repositories[ $repositoryId ];
        }
        else
        {
            return null;
        }
    }

    /**
     * 
     * @return Traversable
     * @see IteratorAggregate#getIterator()
     */
    public function getIterator()
    {
    	return new ArrayIterator($this->_repositories);
    }
}

# EOF
